package com.cnswhy.cloud.server.comsumer.schedule;

import com.cnswhy.cloud.common.core.utils.SystemClockUtil;
import com.cnswhy.cloud.common.logging.uitls.MDCUtil;
import com.cnswhy.cloud.common.rabbitmq.service.MessageService;
import com.cnswhy.cloud.common.redis.annotation.RedisLock;
import com.cnswhy.cloud.common.redis.service.RedisService;
import com.cnswhy.cloud.server.common.entity.constant.AmqpExchange;
import com.cnswhy.cloud.server.common.entity.mq.SpellDayendCancelMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class CustomerTimer {
    @Autowired
    private MessageService messageService;
    @Autowired
    RedisService redisService;
    /**
     * 22点开始处理没拼团成功
     */
    @SuppressWarnings("unchecked")
    @Scheduled(cron = "0 5 22 * * ?")
    @RedisLock(name = "doFailSpell")
    public void doFailSpell(){
        SpellDayendCancelMsg spellDayendCancelMsg1 = (SpellDayendCancelMsg)redisService.get("doFailSpell" + SystemClockUtil.getSettDateyyyyMMdd());
        if(spellDayendCancelMsg1 == null){
            redisService.set("doFailSpell" + SystemClockUtil.getSettDateyyyyMMdd(),SystemClockUtil.getSettDateyyyyMMdd(),300L);
        }else{
            return;
        }
        SpellDayendCancelMsg spellDayendCancelMsg = SpellDayendCancelMsg.builder().settDate(SystemClockUtil.getSettDateyyyyMMdd()).build();
        spellDayendCancelMsg.setTraceId(MDCUtil.getTraceId());
        messageService.sendQueueByObject(AmqpExchange.SPELL_DAYEND_MESSAGE,spellDayendCancelMsg);
    }
    @Scheduled(cron = "0 30 22 * * ?")
    @RedisLock(name = "report")
    public void report(){
        SpellDayendCancelMsg spellDayendCancelMsg1 = (SpellDayendCancelMsg)redisService.get("report" + SystemClockUtil.getSettDateyyyyMMdd());
        if(spellDayendCancelMsg1 == null){
            redisService.set("report" + SystemClockUtil.getSettDateyyyyMMdd(),SystemClockUtil.getSettDateyyyyMMdd(),300L);
        }else{
            return;
        }
        SpellDayendCancelMsg spellDayendCancelMsg = SpellDayendCancelMsg.builder().settDate(SystemClockUtil.getSettDateyyyyMMdd()).build();
        spellDayendCancelMsg.setTraceId(MDCUtil.getTraceId());
        messageService.sendQueueByObject(AmqpExchange.REPORT_MESSAGE,spellDayendCancelMsg);
    }
    @Scheduled(cron = "0 59 09 * * ?")
    @RedisLock(name = "clearSpellValue")
    public void clearSpellValue(){
        SpellDayendCancelMsg spellDayendCancelMsg1 = (SpellDayendCancelMsg)redisService.get("clearSpellValue" + SystemClockUtil.getSettDateyyyyMMdd());
        if(spellDayendCancelMsg1 == null){
            redisService.set("clearSpellValue" + SystemClockUtil.getSettDateyyyyMMdd(),SystemClockUtil.getSettDateyyyyMMdd(),300L);
        }else{
            return;
        }
        SpellDayendCancelMsg spellDayendCancelMsg = SpellDayendCancelMsg.builder().settDate(SystemClockUtil.getSettDateyyyyMMdd()).build();
        spellDayendCancelMsg.setTraceId(MDCUtil.getTraceId());
        messageService.sendQueueByObject(AmqpExchange.CLEAR_SPELL_VALUE_MESSAGE,spellDayendCancelMsg);
    }
}
